package com.tiancheng.trade.settlement.controller.api;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.ApplicationException;
import com.tiancheng.trade.settlement.model.settlement.SettlementRule;
import com.tiancheng.trade.settlement.service.settlement.ISettlementRuleService;
import com.tiancheng.trade.settlement.vo.settlement.api.GetSettlementRuleVO;
import com.tiancheng.trade.settlement.vo.settlement.api.OpenAddRuleVO;
import com.tiancheng.trade.settlement.vo.settlement.api.OpenDeleteRuleVO;
import com.tiancheng.trade.settlement.vo.settlement.api.SettlementRuleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 清分规则
 *
 * @author lzh
 */
@RestController
@RequestMapping("/api/settlementrule")
@Validated
@Slf4j
public class SettlementRuleController {

    @Autowired
    ISettlementRuleService settlementService;


    /**
     * 查询清分规则信息
     *
     * @param settlementRuleVO
     * @return
     */
    @GetMapping("/findSettlementRuleList")
    public Result findSettlementRuleList(GetSettlementRuleVO settlementRuleVO) throws ApplicationException {
        List<SettlementRule> result = settlementService.getSettlementRuleList(settlementRuleVO);
        return Result.success(result);
    }

    /**
     * 修改组织名称
     *
     * @param settlementRule
     * @return
     */
    @PostMapping("/updateMerchantName")
    public Result updateMerchantName(@RequestBody SettlementRule settlementRule) throws ApplicationException {
        int res = settlementService.updateMerchantName(settlementRule);
        return Result.success(res);
    }

    /**
     * 按产品编号查询清分规则
     *
     * @param settlementRule
     * @return
     * @throws ApplicationException
     */
    @GetMapping("/findSettlement")
    public Result findSettlement(SettlementRule settlementRule) throws ApplicationException {
        SettlementRuleVO ruleVO = new SettlementRuleVO();
        ruleVO.setProductCode(settlementRule.getProductCode());
        SettlementRule rule = settlementService.getSettlementByCodeLevel(ruleVO);
        return Result.success(rule);
    }

    /**
     * 根据交易中台商品编码批量查询清分规则
     *
     * @param productCodes
     * @return
     */
    @PostMapping("/getRulesByProductCodes")
    public Result<List<SettlementRule>> getRulesByProductCodes(@RequestBody List<String> productCodes) throws ApplicationException {
        List<SettlementRule> rules = settlementService.getRulesByProductCodes(productCodes);
        return Result.success(rules);
    }

    /**
     * 开放添加清分规则接口
     *
     * @param openAddRuleVO
     * @return
     */
    @PostMapping("/openAddRule")
    public Result<String> openAddRule(@RequestBody @Valid OpenAddRuleVO openAddRuleVO) throws ApplicationException {
        String ruleCode = settlementService.addRule(openAddRuleVO);
        return Result.success(ruleCode);
    }

    /**
     * 开放删除清分规则接口
     *
     * @param openDeleteRuleVO
     * @return
     */
    @PostMapping("/openDeleteRule")
    public Result<String> openDeleteRule(@RequestBody @Valid OpenDeleteRuleVO openDeleteRuleVO) throws ApplicationException {
        settlementService.openDeleteRule(openDeleteRuleVO);
        return Result.success("删除成功");
    }

}
